package com.chengzheng.crawler.spiderman.pojo;

import java.util.Comparator;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * @author zhengcheng
 * @date 2018/1/30
 * @time 下午6:03
 * @email chengzhengzheng@meituan.com
 **/

public class QueueManager {
    volatile static private boolean isWorking = false;

    public static synchronized void setWorkState(boolean state) {
        isWorking = state;
    }

    public static synchronized boolean getWorkState() {
        return isWorking;
    }

    private static final int queueCapacity = 3000;

    public enum QueueType {
        FETCH, PARSE, SAVE, RESULT, DABAI
    }

    private static Map<QueueType, BlockingQueue<Object>> queueMap = new ConcurrentHashMap<>();

    static {
        for (QueueType queueType : QueueType.values()) {
            queueMap
                    .put(queueType, new PriorityBlockingQueue<>(queueCapacity, new QueueCompartor()));
        }
    }

    private static Object poll(QueueType queueType) {
        return queueMap.get(queueType).poll();
    }

    private static boolean offer(QueueType queueType, Object object) {
        return queueMap.get(queueType).offer(object);
    }

    public static Object getFetch() {
        return poll(QueueType.FETCH);
    }

    public static boolean setFetch(Object object) {
        return offer(QueueType.FETCH, object);
    }

    public static Object getParse() {
        return poll(QueueType.PARSE);
    }

    public static boolean setParse(Object object) {
        return offer(QueueType.PARSE, object);
    }

    public static Object getSave() {
        return poll(QueueType.SAVE);
    }

    public static boolean setSave(Object object) {
        return offer(QueueType.SAVE, object);
    }

    public static Object getResult() {
        return poll(QueueType.RESULT);
    }

    public static boolean setResult(Object object) {
        return offer(QueueType.RESULT, object);
    }

    public static Object getDabai() {
        return poll(QueueType.DABAI);
    }

    public static boolean setDabai(Object object) {
        return offer(QueueType.DABAI, object);
    }


    public static boolean isEmpty() {
        return queueMap.get(QueueType.FETCH).isEmpty()

                && queueMap.get(QueueType.PARSE).isEmpty()
                && queueMap.get(QueueType.RESULT).isEmpty();
    }

    private static class QueueCompartor implements Comparator<Object> {

        @Override
        public int compare(Object o1, Object o2) {
            int r = 0;
            if (o1 instanceof Task && o2 instanceof Task) {
                Task t1 = (Task) o1;
                Task t2 = (Task) o2;
                r = t2.getLevel() - t1.getLevel();
                // 创建时间越早，任务优先级越高
                if (0 == r) {
                    if (t1.getCreateTime() < t2.getCreateTime()) {
                        r = -1;
                    } else if (t1.getCreateTime() > t2.getCreateTime()) {
                        r = 1;
                    }
                }
            }
            return r;
        }
    }
}
